package com.primecodegen.common.DataModelAttributes;

/**
 *----------------------------------------------------------
 *----------------------------------------------------------
 * @author Mohammad Awwad
 * Version: v0.0
 * contact: mhmdawwad@gmail.com
 *-----------------------------------------------------------
 *-----------------------------------------------------------
 */
import java.sql.*;
import java.util.*;
import com.primecodegen.common.base.*;
import com.primecodegen.util.*;
import java.util.logging.*;

public class DataModelAttributesDAO extends BaseDAO {

    protected static String TblName = "Data_Model_Attributes";
    protected java.sql.Connection dataModelAttributesConn;
    protected static final Logger logger = Logger.getLogger(DataModelAttributesDAO.class.getName());

    public DataModelAttributesDAO() {
        super();
    }

    public int insert(IBaseTable dataModelAttributesTable) throws Exception {


        final boolean isConnSupplied = (dataModelAttributesConn != null);
        DataModelAttributesTable newRecord = (DataModelAttributesTable) dataModelAttributesTable;
        int insertValue = ComConstants.UNREALISTIC_INT;
        Connection con = null;
        PreparedStatement PrepSt = null;
        String sInsertSQL = null;
        StringBuffer sbSQL = new StringBuffer();

        if (dataModelAttributesTable == null) {
            throw new Exception("DAOException in DataModelAttributesDAO class :dataModelAttributesTable is Null");
        }
        sInsertSQL = "INSERT INTO " + DataModelAttributesDAO.TblName + " (";
        try {
            con = isConnSupplied ? dataModelAttributesConn : ResourceManager.getConnection();

            String sComma = "";
            int iColCount = 0;


            if (!util.isEmpty(newRecord.getID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.ID);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getCode())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.CODE);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getDataType())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.DATATYPE);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getPrimaryKey())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.PRIMARYKEY);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getForeignKey())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.FOREIGNKEY);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getDataModelObject_ID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(DataModelAttributesDBCols.DATAMODELOBJECT_ID);
                sComma = ",";
            }


            sbSQL.append(") VALUES (");
            sComma = "";
            for (int i = 0; i < iColCount; i++) {
                sbSQL.append(sComma).append("?");
                sComma = ",";
            }
            sbSQL.append(");");

            sInsertSQL = sInsertSQL + sbSQL.toString();

            PrepSt = con.prepareStatement(sInsertSQL,
                    Statement.RETURN_GENERATED_KEYS);
            int i = 1;



            if (!util.isEmpty(newRecord.getID())) {
                PrepSt.setInt(i++, newRecord.getID());
            }

            if (!util.isEmpty(newRecord.getCode().trim())) {
                PrepSt.setString(i++, newRecord.getCode().trim());
            }

            if (!util.isEmpty(newRecord.getDataType().trim())) {
                PrepSt.setString(i++, newRecord.getDataType().trim());
            }

            if (!util.isEmpty(newRecord.getPrimaryKey().trim())) {
                PrepSt.setString(i++, newRecord.getPrimaryKey().trim());
            }

            if (!util.isEmpty(newRecord.getForeignKey().trim())) {
                PrepSt.setString(i++, newRecord.getForeignKey().trim());
            }

            if (!util.isEmpty(newRecord.getDataModelObject_ID())) {
                PrepSt.setInt(i++, newRecord.getDataModelObject_ID());
            }


            insertValue = PrepSt.executeUpdate();
            ResultSet rsIDJustInserted = PrepSt.getGeneratedKeys();
            if (rsIDJustInserted.next()) {
                insertValue = rsIDJustInserted.getInt(1);
            }



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in DataModelAttributesDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in DataModelAttributesDAO.insert method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in DataModelAttributesDAO.insert method Message: {0}", e.getMessage());
            throw new Exception("Exception in DataModelAttributesDAO.insert method");
        } finally {
            ResourceManager.close(PrepSt);
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return insertValue;
    }

    public int deleteByID(int IDPar) throws Exception {

        final boolean isConnSupplied = (dataModelAttributesConn != null);
        int ID = (int) IDPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        int noOfRowsDeleted = ComConstants.UNREALISTIC_INT;
        String deleteSQL = "DELETE FROM " + DataModelAttributesDAO.TblName + " WHERE " + DataModelAttributesDBCols.ID + " = ?";
        try {
            con = isConnSupplied ? dataModelAttributesConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(deleteSQL);
            stmnt.setInt(1, ID);

            noOfRowsDeleted = stmnt.executeUpdate();



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in DataModelAttributesDAO.DeleteByID method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in DataModelAttributesDAO.deleteByID method"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in DataModelAttributesDAO.DeleteByID method Message: {0}", e.getMessage());
            throw new Exception("Exception in DataModelAttributesDAO.deleteByID method"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return noOfRowsDeleted;
    }

    public int updateByCriterion(IBaseTable dataModelAttributesSrchCriterionDTOPar,
            IBaseTable updatedRecordPar) throws Exception {

        final boolean isConnSupplied = (dataModelAttributesConn != null);
        DataModelAttributesTable dataModelAttributesSrchCriterionDTO = (DataModelAttributesTable) dataModelAttributesSrchCriterionDTOPar;
        DataModelAttributesTable updatedRecord = (DataModelAttributesTable) updatedRecordPar;
        int iNoOfRecordsUpdated = -1;
        Connection con = null;
        PreparedStatement stmnt = null;
        try {

            con = isConnSupplied ? dataModelAttributesConn : ResourceManager.getConnection();
            StringBuffer sbUpdateSQLStmnt = new StringBuffer();
            String sComma = "";
            sbUpdateSQLStmnt.append("UPDATE ").append(DataModelAttributesDAO.TblName).append(" SET ");



            if (!util.isEmpty(updatedRecord.getID())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.ID + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getCode())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.CODE + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getDataType())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.DATATYPE + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getPrimaryKey())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.PRIMARYKEY + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getForeignKey())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.FOREIGNKEY + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getDataModelObject_ID())) {
                sbUpdateSQLStmnt.append(sComma).append(DataModelAttributesDBCols.DATAMODELOBJECT_ID + " = ").append("?");
                sComma = ",";
            }


            sbUpdateSQLStmnt.append(getUpdateWhereClause(dataModelAttributesSrchCriterionDTO));

            stmnt = con.prepareStatement(sbUpdateSQLStmnt.toString());
            int i = 1;

            if (!util.isEmpty(updatedRecord.getID())) {
                stmnt.setInt(i++, updatedRecord.getID());
            }

            if (!util.isEmpty(updatedRecord.getCode())) {
                stmnt.setString(i++, updatedRecord.getCode());
            }

            if (!util.isEmpty(updatedRecord.getDataType())) {
                stmnt.setString(i++, updatedRecord.getDataType());
            }

            if (!util.isEmpty(updatedRecord.getPrimaryKey())) {
                stmnt.setString(i++, updatedRecord.getPrimaryKey());
            }

            if (!util.isEmpty(updatedRecord.getForeignKey())) {
                stmnt.setString(i++, updatedRecord.getForeignKey());
            }

            if (!util.isEmpty(updatedRecord.getDataModelObject_ID())) {
                stmnt.setInt(i++, updatedRecord.getDataModelObject_ID());
            }



            iNoOfRecordsUpdated = stmnt.executeUpdate();

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in DataModelAttributesDAO.updateByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in DataModelAttributesDAO.updateByCriterion method ");
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return iNoOfRecordsUpdated;
    }

    public int updateByID(IBaseTable newRecordPar) throws Exception {
        DataModelAttributesTable newRecord = (DataModelAttributesTable) newRecordPar;
        if (newRecord != null) {
            DataModelAttributesTable contentsCategorySrchCriterionDTO = new DataModelAttributesTable();
            contentsCategorySrchCriterionDTO.setID(newRecord.getID());
            return this.updateByCriterion(contentsCategorySrchCriterionDTO, newRecord);
        } else {
            logger.log(Level.WARNING, "Exception in method DataModelAttributesDAO.update.newRecord passed is null.");
            throw new Exception(
                    "Exception in method DataModelAttributesDAO.update.newRecord passed is null.");
        }
    }

    private String getUpdateWhereClause(DataModelAttributesTable dataModelAttributesSrchCriterionDTO) {
        String sWhere = "";
        if (dataModelAttributesSrchCriterionDTO.getID() != ComConstants.UNREALISTIC_INT) {
            sWhere = " WHERE ID = " + dataModelAttributesSrchCriterionDTO.getID();
        }
        return sWhere;
    }

    public IBaseTable getByID(int ID) throws Exception {

        if (!util.isEmpty(ID)) {
            DataModelAttributesTable dataModelAttributesSrchCriterionDTO = new DataModelAttributesTable();
            dataModelAttributesSrchCriterionDTO.setID(ID);
            dataModelAttributesSrchCriterionDTO.setFilter(true);
            ArrayList<DataModelAttributesTable> dataModelAttributesTables = this.getByCriterionOrderedBy(
                    dataModelAttributesSrchCriterionDTO, null);
            DataModelAttributesTable dataModelAttributesTable = null;
            if (dataModelAttributesTables != null) {
                Iterator<DataModelAttributesTable> it = dataModelAttributesTables.iterator();
                while (it.hasNext()) {
                    dataModelAttributesTable = (DataModelAttributesTable) it.next();
                }
            }

            return dataModelAttributesTable;
        } else {
            logger.log(Level.WARNING, "Exception in method DataModelAttributesDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method DataModelAttributesDAO.getByID.ID passed is unrealistic.");
        }
    }

    public boolean deleteDataModelObjectIsSafe(int dataModelObjectID) throws Exception {
        ArrayList<DataModelAttributesTable> dataModelAttributesTables = new ArrayList<DataModelAttributesTable>();
        if (!util.isEmpty(dataModelObjectID)) {
            DataModelAttributesTable dataModelAttributesSrchCriterionDTO = new DataModelAttributesTable();
            dataModelAttributesSrchCriterionDTO.setDataModelObject_ID(dataModelObjectID);
            dataModelAttributesSrchCriterionDTO.setFilter(true);
            dataModelAttributesTables = this.getByCriterionOrderedBy(dataModelAttributesSrchCriterionDTO, null);

            if (dataModelAttributesTables.size() > 0) {
                return false;//Delete is SAFE, Group Empty
            } else {
                return true;
            }

        } else {
            logger.log(Level.WARNING, "Exception in method DataModelAttributesDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method DataModelAttributesDAO.getByID.ID passed is unrealistic.");
        }
    }

    public ArrayList<DataModelAttributesTable> getByCriterionOrderedBy(
            IBaseTable dataModelAttributesSrchCriterionDTOPar, ColToOrderBy colToOrderBy)
            throws Exception {


        final boolean isConnSupplied = (dataModelAttributesConn != null);
        DataModelAttributesTable dataModelAttributesSrchCriterionDTO = (DataModelAttributesTable) dataModelAttributesSrchCriterionDTOPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        ResultSet rs = null;

        String sWhereClause = "";

        String sOrderBy = "";
        if (colToOrderBy != null && colToOrderBy.Name != null) {
            sOrderBy = this.getOrderBy(colToOrderBy);
        }
        if (dataModelAttributesSrchCriterionDTO != null && dataModelAttributesSrchCriterionDTO.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(dataModelAttributesSrchCriterionDTO);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" DataModelAttributes.getByCriterionOrderedBy.Exception:  " + e.getMessage());
            }

            if (sWhereClause != null && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }
        }
        ArrayList<DataModelAttributesTable> dataModelAttributesTables = new ArrayList<DataModelAttributesTable>();

        try {
            con = isConnSupplied ? dataModelAttributesConn : ResourceManager.getConnection();

          //  String sSelectSQL = "SELECT * FROM   " + TblName;
            StringBuilder sSelectSQL = new StringBuilder();
            sSelectSQL
                   .append("SELECT ")//
                   .append("  data_model_attributes.ID ")
                   .append(",  data_model_attributes.Code ")
                   .append(",  data_model_attributes.DataType ")
                   .append(",  data_model_attributes.PrimaryKey ")
                   .append(",  data_model_attributes.ForeignKey ")
                   .append(",  data_model_attributes.DataModelObjectID ")
                   .append(",  data_model_object.Code ")
                   .append(" FROM ")
                   .append(" new_src_code_gen.data_model_attributes data_model_attributes ")
                   .append("INNER JOIN new_src_code_gen.data_model_object data_model_object ON (data_model_attributes.DataModelObjectID = data_model_object.ID)");

            stmnt = con.prepareStatement(sSelectSQL + sWhereClause + sOrderBy);




            if (dataModelAttributesSrchCriterionDTO != null && dataModelAttributesSrchCriterionDTO.isFilter()) {
                int colCount = 1;


                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getID())) {
                    stmnt.setInt(colCount++, dataModelAttributesSrchCriterionDTO.getID());
                }

                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getCode())) {
                    stmnt.setString(colCount++, dataModelAttributesSrchCriterionDTO.getCode());
                }

                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getDataType())) {
                    stmnt.setString(colCount++, dataModelAttributesSrchCriterionDTO.getDataType());
                }

                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getPrimaryKey())) {
                    stmnt.setString(colCount++, dataModelAttributesSrchCriterionDTO.getPrimaryKey());
                }

                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getForeignKey())) {
                    stmnt.setString(colCount++, dataModelAttributesSrchCriterionDTO.getForeignKey());
                }

                if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getDataModelObject_ID())) {
                    stmnt.setInt(colCount++, dataModelAttributesSrchCriterionDTO.getDataModelObject_ID());
                }

            }

            rs = stmnt.executeQuery();
            DataModelAttributesTable dataModelAttributesTable = null;

            while (rs.next()) {
                dataModelAttributesTable = new DataModelAttributesTable();



                dataModelAttributesTable.setID(rs.getInt("data_model_attributes.ID"));
                dataModelAttributesTable.setCode(util.removeNullFrom(rs.getString("data_model_attributes.Code")));
                dataModelAttributesTable.setDataType(util.removeNullFrom(rs.getString("data_model_attributes.DataType")));
                dataModelAttributesTable.setPrimaryKey(util.removeNullFrom(rs.getString("data_model_attributes.PrimaryKey")));
                dataModelAttributesTable.setForeignKey(util.removeNullFrom(rs.getString("data_model_attributes.ForeignKey")));
                dataModelAttributesTable.setDataModelObject_Name(util.removeNullFrom(rs.getString("data_model_object.Code")));
                dataModelAttributesTable.setDataModelObject_ID(rs.getInt("data_model_attributes.DataModelObjectID"));


                dataModelAttributesTables.add(dataModelAttributesTable);
                dataModelAttributesTable = null;
            }

        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in DataModelAttributesDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in method:DataModelAttributesDAO.getByCriterionOrderedBy"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in DataModelAttributesDAO.insert method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in method:DataModelAttributesDAO.getByCriterionOrderedBy"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return dataModelAttributesTables;
    }

    private String getRetrieveWhereClause(DataModelAttributesTable dataModelAttributesSrchCriterionDTO)
            throws Exception {


        StringBuffer sbWhereClause = new StringBuffer();
        String sAnd = "";
        boolean bPrevColExistsInWhereClause = false;
        boolean bAtLeastOneFieldExists = false;

        if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.getID())) {
            bAtLeastOneFieldExists = true;
            sbWhereClause.append(" data_model_attributes.ID =").append(" ? ");
            bPrevColExistsInWhereClause = true;
        }

        if (dataModelAttributesSrchCriterionDTO.Opr != null) {



            if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.Opr.getCodeOpr().trim()) && !util.isEmpty(dataModelAttributesSrchCriterionDTO.getCode().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (dataModelAttributesSrchCriterionDTO.Opr.getCodeOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.CODE + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.CODE + "  ").append(dataModelAttributesSrchCriterionDTO.Opr.getCodeOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.Opr.getDataTypeOpr().trim()) && !util.isEmpty(dataModelAttributesSrchCriterionDTO.getDataType().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (dataModelAttributesSrchCriterionDTO.Opr.getDataTypeOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.DATATYPE + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.DATATYPE + "  ").append(dataModelAttributesSrchCriterionDTO.Opr.getDataTypeOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.Opr.getPrimaryKeyOpr().trim()) && !util.isEmpty(dataModelAttributesSrchCriterionDTO.getPrimaryKey().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (dataModelAttributesSrchCriterionDTO.Opr.getPrimaryKeyOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.PRIMARYKEY + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.PRIMARYKEY + "  ").append(dataModelAttributesSrchCriterionDTO.Opr.getPrimaryKeyOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.Opr.getForeignKeyOpr().trim()) && !util.isEmpty(dataModelAttributesSrchCriterionDTO.getForeignKey().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (dataModelAttributesSrchCriterionDTO.Opr.getForeignKeyOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.FOREIGNKEY + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + DataModelAttributesDBCols.FOREIGNKEY + "  ").append(dataModelAttributesSrchCriterionDTO.Opr.getForeignKeyOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(dataModelAttributesSrchCriterionDTO.Opr.getDataModelObject_IDOpr().trim()) && !util.isEmpty(dataModelAttributesSrchCriterionDTO.getDataModelObject_ID())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                sbWhereClause.append(sAnd).append(" data_model_attributes.DataModelObjectID  ").append(dataModelAttributesSrchCriterionDTO.Opr.getDataModelObject_IDOpr().trim()).append("?");
                bPrevColExistsInWhereClause = true;
            }




        }

        if (sbWhereClause.toString().trim().length() <= 0) {
            logger.log(Level.WARNING, "getRetrieveWhereClause Cannot return Null  ");
            throw new Exception("getRetrieveWhereClause Cannot return Null  ");
        } else {
            return sbWhereClause.toString().trim();
        }
    }

    private String getOrderBy(ColToOrderBy colToOrderBy) {
        String sOrderBy = " Order By ";

        sOrderBy = sOrderBy + colToOrderBy.Name;

        if (colToOrderBy.OrderType != null) {
            sOrderBy = sOrderBy + " " + colToOrderBy.OrderType;
        }
        return sOrderBy;
    }

    public int deleteByCriterion(IBaseTable recordsToDeleteCritPar)
            throws Exception {

        final boolean isConnSupplied = (dataModelAttributesConn != null);
        DataModelAttributesTable recordsToDeleteCrit = (DataModelAttributesTable) recordsToDeleteCritPar;
        int iNoOfRecordsDeleted = -1;
        Connection con = null;
        PreparedStatement stmnt = null;

        StringBuffer sbDeleteSQLStmnt = new StringBuffer();
        sbDeleteSQLStmnt.append("DELETE from ").append(DataModelAttributesDAO.TblName).append(" WHERE ");

        String sWhereClause = "";
        if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(recordsToDeleteCrit);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" DataModelAttributes.deleteByCriterion.Exception:  "
                        + e.getMessage());
            }

            if (sWhereClause != null
                    && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }

            sbDeleteSQLStmnt.append(sWhereClause);
        }
        try {
            con = isConnSupplied ? dataModelAttributesConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(sbDeleteSQLStmnt.toString());

            if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            }
            //  iNoOfRecordsDeleted = stmnt.executeUpdate ();



            return iNoOfRecordsDeleted;
        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in DataModelAttributesDAO.deleteByCriterion method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in DataModelAttributesDAO.deleteByCriterion method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in DataModelAttributesDAO.deleteByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in DataModelAttributesDAO.deleteByCriterion method");
        } finally {

            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
    }
}
